﻿#region Apache License Version 2.0
/*----------------------------------------------------------------

Copyright 2025 Suzhou Senparc Network Technology Co.,Ltd.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions
and limitations under the License.

Detail: https://github.com/Senparc/Senparc.CO2NET/blob/master/LICENSE

----------------------------------------------------------------*/
#endregion Apache License Version 2.0

/*----------------------------------------------------------------
    Copyright (C) 2025 Senparc

    FileName：LocalObjectCacheHelper.cs
    File Function Description：Global static data source helper class.


    Creation Identifier：Senparc - 20160308

 ----------------------------------------------------------------*/

#if !NET462
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
#endif
using Senparc.CO2NET.Exceptions;
using System;
using System.Collections.Generic;


namespace Senparc.CO2NET.Cache
{
    /// <summary>
    /// Global static data source helper class
    /// </summary>
    public static class LocalObjectCacheHelper
    {
#if NET462
        /// <summary>
        /// List of all data collections
        /// </summary>
        public static System.Web.Caching.Cache LocalObjectCache { get; set; }

        static LocalObjectCacheHelper()
        {
            LocalObjectCache = System.Web.HttpRuntime.Cache;
        }
#else

        private static IMemoryCache _localObjectCache;

        /// <summary>
        /// List of all data collections
        /// </summary>
        public static IMemoryCache LocalObjectCache
        {
            get
            {
                if (_localObjectCache == null)
                {

                    if (_localObjectCache == null)
                    {
                        try
                        {
                            var serviceProvider = SenparcDI.GetServiceProvider();
                            _localObjectCache = serviceProvider.GetService<IMemoryCache>();
                        }
                        catch
                        {
                            throw new CacheException("IMemoryCache 依赖注入未设置！如果您使用的是本地缓存，请确保在 Startup.cs 中执行了 services.AddMemoryCache() 方法！");
                        }
                    }
                }
                return _localObjectCache;
            }
        }

        /// <summary>
        /// .NET Core's MemoryCache does not provide a method to traverse all items, so this is a place to store Keys
        /// </summary>
        public static Dictionary<string, DateTimeOffset> Keys { get; set; } = new Dictionary<string, DateTimeOffset>();

        static LocalObjectCacheHelper()
        {

        }

        /// <summary>
        /// Get the cache key for storing Keys information
        /// </summary>
        /// <param name="cacheStrategy"></param>
        /// <returns></returns>
        public static string GetKeyStoreKey(BaseCacheStrategy cacheStrategy)
        {
            var keyStoreFinalKey = cacheStrategy.GetFinalKey("CO2NET_KEY_STORE");
            return keyStoreFinalKey;
        }
#endif
    }
}
